package edu.cmu.cs.task7.model;

import com.ebay.sdk.ApiContext;
import com.ebay.sdk.ApiException;
import com.ebay.sdk.SdkException;
import com.ebay.sdk.call.EndItemCall;

import com.ebay.sdk.helper.ui.ControlTagItem;
import com.ebay.sdk.util.eBayUtil;
import com.ebay.soap.eBLBaseComponents.EndReasonCodeType;


public class EndItemModel{
	 
	private String itemId;
	private ApiContext apiContext=new ApiContext();
	private String EndReason; 

	 public EndItemModel(ApiContext apiContext, String itemId,String EndReason) throws Exception{
		 this.apiContext=apiContext;
		 this.itemId=itemId;
		 this.EndReason=EndReason;
     }

	 public String endItem() throws ApiException, SdkException, Exception{
		 EndItemCall api = new EndItemCall(apiContext);
		 api.setItemID(itemId);
		 
		 if( itemId.length() == 0 )
		        return "Please enter item ID.";
		 
		 
	     ControlTagItem ct;
	     if(EndReason.equalsIgnoreCase("Incorrect")){
             ct = new ControlTagItem("Incorrect", EndReasonCodeType.INCORRECT);
	     }else if(EndReason.equalsIgnoreCase("LostOrBroken")){
	    	 ct= new ControlTagItem("LostOrBroken", EndReasonCodeType.LOST_OR_BROKEN);
	     }else if(EndReason.equalsIgnoreCase("NotAvailable")){
	    	 ct= new ControlTagItem("NotAvailable", EndReasonCodeType.NOT_AVAILABLE); 
	     }else if(EndReason.equalsIgnoreCase("OtherListingError")){
	    	 ct= new ControlTagItem("OtherListingError", EndReasonCodeType.OTHER_LISTING_ERROR);
	     }else{ //The default
	    	 ct = new ControlTagItem("Incorrect", EndReasonCodeType.INCORRECT);
	     }
	    	 
	       
	     api.setEndingReason((EndReasonCodeType)ct.Tag);
		 
		 try{
			 java.util.Calendar endTime = api.endItem();
			 return eBayUtil.toAPITimeString(endTime.getTime());
		 }catch (ApiException e){
			 return e.toString();
		 }
		 
    }
}
